<?php
// +----------------------------------------------------------------------
// | 有理想的地方，地狱都是天堂。
// +----------------------------------------------------------------------
// | Copyright @ df81.com 版权所有
// +----------------------------------------------------------------------
// | 作者: 南国老符 邮箱：1062129401@qq.com
// +----------------------------------------------------------------------

namespace app\admin\controller;
use think\exception\ValidateException;
use app\admin\validate\System as systemValidate;
use think\facade\Db;
use think\facade\View;

class System extends Base{
    // 存储设置
    public function storage(){
        if(!request()->isPost()){
            //获取水印字体
            $waterPath = app()->getRootPath().'vendor/topthink/think-captcha/assets/ttfs/';
            $dofile = new \common\Dofile();
            $fontList = $dofile->getFiles($waterPath,false,'ttf');
            View::assign('font_list',$fontList);
            
            return view();
        }else{
            // 表单验证
            try {
                validate(systemValidate::class)->check(input('post.'));
            } catch (ValidateException $e) {
                return json(['code' => 1001, 'msg' => $e->getMessage(), 'data' => '']);
            }

            // 七牛云
            $qiniu_access_key = input('qiniu_access_key');
            if($qiniu_access_key == '******') $qiniu_access_key = config('filesystem.disks.qiniu.accessKey');
            $qiniu_secret_key = input('qiniu_secret_key');
            if($qiniu_secret_key == '******') $qiniu_secret_key = config('filesystem.disks.qiniu.secretKey');
            $qiniu_bucket = input('qiniu_bucket');
            if($qiniu_bucket == '******') $qiniu_bucket = config('filesystem.disks.qiniu.bucket');
            $qiniu_url = input('qiniu_url');
            if($qiniu_url == '******') $qiniu_url = config('filesystem.disks.qiniu.domain');

            // 阿里云
            $aliyun_access_id = input('aliyun_access_id');
            if($aliyun_access_id == '******') $aliyun_access_id = config('filesystem.disks.aliyun.accessId');
            $aliyun_access_secret = input('aliyun_access_secret');
            if($aliyun_access_secret == '******') $aliyun_access_secret = config('filesystem.disks.aliyun.accessSecret');
            $aliyun_bucket = input('aliyun_bucket');
            if($aliyun_bucket == '******') $aliyun_bucket = config('filesystem.disks.aliyun.bucket');
            $aliyun_endpoint = input('aliyun_endpoint');
            if($aliyun_endpoint == '******') $aliyun_endpoint = config('filesystem.disks.aliyun.endpoint');
            $aliyun_url = input('aliyun_url');
            if($aliyun_url == '******') $aliyun_url = config('filesystem.disks.aliyun.url');

            // 腾讯云
            $qcloud_region = input('qcloud_region');
            if($qcloud_region == '******') $qcloud_region = config('filesystem.disks.qcloud.region');
            $qcloud_appid = input('qcloud_appid');
            if($qcloud_appid == '******') $qcloud_appid = config('filesystem.disks.qcloud.app_id');
            $qcloud_secretid = input('qcloud_secretid');
            if($qcloud_secretid == '******') $qcloud_secretid = config('filesystem.disks.qcloud.secret_id');
            $qcloud_secret_key = input('qcloud_secret_key');
            if($qcloud_secret_key == '******') $qcloud_secret_key = config('filesystem.disks.qcloud.secret_key');
            $qcloud_bucket = input('qcloud_bucket');
            if($qcloud_bucket == '******') $qcloud_bucket = config('filesystem.disks.qcloud.bucket');
            $qcloud_cdn = input('qcloud_cdn');
            if($qcloud_cdn == '******') $qcloud_cdn = config('filesystem.disks.qcloud.cdn');

            $filesystem_str = "<?php
                return [
                    // 默认磁盘
                    'default' => env('filesystem.driver', 'local'),
                    // 磁盘列表
                    'disks'   => [
                        'local'  => [
                            'type' => 'local',
                            'root' => app()->getRuntimePath() . 'storage',
                        ],
                        'public' => [
                            // 磁盘类型
                            'type'       => 'local',
                            // 磁盘路径
                            'root'       => './uploads',
                            // 磁盘路径对应的外部URL路径
                            'url'        => '/storage',
                            // 可见性
                            'visibility' => 'public',
                        ],
                        // 云存储OSS
                        'qiniu'  => [
                            'type'      => 'qiniu',
                            'accessKey' => '".$qiniu_access_key."',
                            'secretKey' => '".$qiniu_secret_key."',
                            'bucket'    => '".$qiniu_bucket."',
                            'domain'       => '".$qiniu_url."',//不要斜杠结尾，此处为URL地址域名。
                        ],
                        'aliyun' => [
                            'type'         => 'aliyun',
                            'accessId'     => '".$aliyun_access_id."',
                            'accessSecret' => '".$aliyun_access_secret."',
                            'bucket'       => '".$aliyun_bucket."',
                            'endpoint'     => '".$aliyun_endpoint."',
                            'url'          => '".$aliyun_url."',//不要斜杠结尾，此处为URL地址域名。
                        ],
                        'qcloud' => [
                            'type'       => 'qcloud',
                            'region'      => '".$qcloud_region."', //bucket 所属区域 英文
                            'app_id'      => '".$qcloud_appid."', // 域名中数字部分
                            'secret_id'   => '".$qcloud_secretid."',
                            'secret_key'  => '".$qcloud_secret_key."',
                            'bucket'          => '".$qcloud_bucket."',
                            'timeout'         => ".input('qcloud_timeout').",
                            'connect_timeout' => ".input('qcloud_connect_timeout').",
                            'cdn'             => '".$qcloud_cdn."',
                            'scheme'          => '".input('qcloud_scheme')."',
                            'read_from_cdn'   => ".input('qcloud_read_from_cdn').",
                        ],
                    ],
                ];";
            file_put_contents(app()->getConfigPath().'filesystem.php',$filesystem_str);

            $global_str = "<?php
                //前后端共用配置
                return [
                    //系统版本
                    'version' => [
                        // 版本名称
                        'name' => '".config('global.version.name')."',
                        // 版本号
                        'code' => '".config('global.version.code')."',
                    ],
                    
                    //登录失败设置
                    'verify_times' => '".config('global.verify_times')."',
                    'lock_times' => '".config('global.lock_times')."',
                    'lock_minute' => '".config('global.lock_minute')."',
                    // ip黑名单
                    'ip_black' =>'".config('global.ip_black')."',
                    // 加密盐值
                    'salt' => '".config('global.salt')."',

                    // URL模式
                    'url' => [
                        'city_site' => '".config('global.url.city_site')."',
                        'bind_lang' => '".config('global.url.bind_lang')."',
                        'view' => '".config('global.url.view')."',
                        'root' => '".config('global.url.root')."',
                        'root_module' => '".config('global.url.root_module')."',
                        'root_type' => '".config('global.url.root_type')."',
                        'category' => '".config('global.url.category')."',
                        'length' => '".config('global.url.length')."',
                        'pinyin_type' => '".config('global.url.pinyin_type')."',
                    ],

                    // 存储模式
                    'storage' => [
                        'type' => '".input('type')."',
                        'imageMaxSize' => '".input('imageMaxSize')."',
                        'fileMaxSize' => '".input('fileMaxSize')."',
                        'upload_filename' => '".input('upload_filename')."',
                    ],

                    // 图片水印等
                    'image' => [
                        'thumbnail' => '".input('thumbnail')."',
                        'thumbwidth' => '".input('thumbwidth')."',
                        'thumbheight' => '".input('thumbheight')."',
                        'thumb' => '".input('thumb')."',
                        'water' => '".input('water')."',
                        'waterPosition' => '".input('waterPosition')."',
                        'waterImage' => '".input('waterImage')."',
                        'transparency' => '".input('transparency')."',
                        'waterText' => '".input('waterText')."',
                        'waterfont' => '".input('waterfont')."',
                        'watercolor' => '".input('watercolor')."',
                        'watersize' => '".input('watersize')."',
                    ],
                ];";
                file_put_contents(app()->getConfigPath().'global.php',$global_str);

			return json(['code' => 200, 'msg' => lang('c_success'), 'data' => '']);
        }		
	}
}